﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;


namespace Bitirme.DAL.Utils
{
    public class SQLUtils
    {
        /// <summary>
        /// Gets a connection
        /// </summary>
        /// <returns>An SqlConnection object</returns>
        public static SqlConnection GetConnection()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["BITIRMEConnectionString"].ConnectionString;
            return con;
        }

        public static DateTime NVL(object value, DateTime defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToDateTime(value);
            }
        }

        public static int NVL(object value, int defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToInt32(value);
            }
        }

        public static byte NVL(object value, byte defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToByte(value);
            }
        }

        public static string NVL(object value, string defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToString(value);
            }
        }

        public static long NVL(object value, long defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToInt64(value);
            }
        }

        public static short NVL(object value, short defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToInt16(value);
            }
        }

        public static bool NVL(object value, bool defaultValue)
        {
            if (value == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToBoolean(value);
            }
        }
    }
}